import { IColumns, StoreFuncs } from "../../ExcelStore";
import { FieldTypeName, dotPngImg, getInputValue, getHreflink, linkedColor } from "./FieldCellType";
import { get } from 'lodash';
import { convertAmountChinese, judgeIsNumber } from "../../../../components/excel-layout/cell-field/input-element/wf-form-input/util";
import $ from 'jquery';
const convertToThousand = (value) => {
    if (!judgeIsNumber(value)) {
        return value;
    }
    const index = value.indexOf(".");
    let intPar: any = index > -1 ? value.substring(0, index) : value;
    let pointPar = index > -1 ? value.substring(index) : "";
    const reg1 = /(-?\d+)(\d{3})/;
    while (reg1.test(intPar)) {
        intPar = intPar.replace(reg1, "$1,$2");
    }
    return intPar + pointPar;
}

const getHtmlText = (value)=>{
    let div = document.createElement('div');
    div.innerHTML = value;
    return div.innerText || div.textContent; 
}
export default (column: IColumns, editable = false) => {
    const { typeName,type } = column;
    const isText = typeName === FieldTypeName.Text;
    const isInt = typeName === FieldTypeName.Int;
    const isFloat = typeName === FieldTypeName.Float;
    const isThousand = typeName === FieldTypeName.Thousand;
    const isMoney = typeName === FieldTypeName.Money;
    class Type extends GC.Spread.Sheets.CellTypes.Text {
        itemWidth = {};
        paint(ctx: CanvasRenderingContext2D, value: any, x: number, y: number, w: number, h: number, style: GC.Spread.Sheets.Style, context?: any) {
            try {
                ctx.clearRect(x, y, w, h);
                if (!editable && getHreflink(value)) {
                    style.foreColor = linkedColor;
                    style.textDecoration = GC.Spread.Sheets.TextDecorationType.underline;
                    const { row } = context;
                    this.itemWidth[row] = parseInt(ctx.measureText(getInputValue(value)).width.toString());
                }
                if(column.column==='modedatacreatedate'){
                    value = {"value":value};
                }
                if(column.column==='modedatamodifydatetime'){
                    value = {"value":value};
                }
                if(column.column==='modelableid'){
                    value = {"value":value};
                }
                if(column.column==='modedatastatus'){
                    value = {"value":value};
                }
                if(column['htmltype']=='2'&&type=='2'){//多行文本html去除标签
                    value={...value,"value":getHtmlText(getInputValue(value))}
                }
                super.paint(ctx,value, x, y, w, h, style, context);
                if (editable) {
                    const { sheet, row, col } = context;
                    if (get(sheet.modifyedValues, `${StoreFuncs.getRandomId(row)}.${col}`)) {
                        ctx.drawImage(dotPngImg, x, y, 8, 8);
                    }
                }
            } catch (e) {
            }
        }
        getAutoFitWidth(value: any, text: string, cellStyle: GC.Spread.Sheets.Style, zoomFactor: number, context?: any): number {
            return super.getAutoFitWidth(value, text, cellStyle, zoomFactor, context);
        }
        createEditorElement(context?: any): any {
            return super.createEditorElement(context);
        }
        setEditorValue(editorContext: any, value: any, context?: any): void {
            const inputValue = getInputValue(value);
            if (isThousand) {
                return super.setEditorValue(editorContext, inputValue ? inputValue.replace(/,/g, '') : inputValue, context)
            } else {
                return super.setEditorValue(editorContext, inputValue, context)
            }
        }
        getEditorValue(editorContext: any, context?: any): any {
            const { sheet, row, col } = context;
            const value = sheet.getValue(row, col);
            const hreflink = getHreflink(value);
            let inputValue = '';
            if (isThousand) {
                inputValue = convertToThousand(super.getEditorValue(editorContext, context));
            } else {
                inputValue = super.getEditorValue(editorContext, context);
            }
            return JSON.stringify({
                hreflink,
                value: inputValue
            })

        }
        format(value: any, format: any, formattedData?: any, context?: any) {
            const inputValue = getInputValue(value);
            if (editable || isText || isInt || isFloat) {
                return inputValue;
            } else if (isThousand) {
                return convertToThousand(inputValue);
            } else if (isMoney) {
                return convertAmountChinese(inputValue);
            }
        }
        processMouseUp(hitInfo: GC.Spread.Sheets.IHitTestCellTypeHitInfo): boolean {
            if (editable) {
                return super.processMouseUp(hitInfo);
            }
            const { sheet, row, col, sheetArea, isReservedLocation } = hitInfo as any;
            if (isReservedLocation) {
                const hreflink = getHreflink(sheet.getValue(row, col, sheetArea));
                if (hreflink) {
                    (window as any).cubeOpenHreflinkFromExcel(hreflink);
                }
            }

            return true;
        }
        getHitInfo(x: number, y: number, cellStyle: GC.Spread.Sheets.Style, cellRect: GC.Spread.Sheets.Rect, context?: any): GC.Spread.Sheets.IHitTestCellTypeHitInfo {
            const { row, col, sheetArea, sheet } = context;
            const info = {
                x,
                y,
                row,
                col,
                cellRect,
                sheetArea,
                sheet,
                isReservedLocation: !editable && x - cellRect.x <= this.itemWidth[row]
            } as any;
            return info;
        }
        processMouseMove(hitInfo: GC.Spread.Sheets.IHitTestCellTypeHitInfo) {
            const { sheet, isReservedLocation } = hitInfo as any;
            var div = sheet.getParent().getHost();
            var canvasId = div.id + "vp_vp";

            var canvas = $("#" + canvasId)[0];
            if (sheet && isReservedLocation) {
                canvas.style.cursor = 'pointer';
                return true;
            } else {
                canvas.title = "";
                canvas.style.cursor = 'default';
            }
            return false;
        };
    }
    return Type;
}